package net.obxlabs.tweetpad.behaviour;

import java.awt.Color;
import net.nexttext.*;
import net.nexttext.behaviour.*;
import net.nexttext.behaviour.standard.*;

/**
 * Randomly colorizes the TextObject.
 * 
 * $Id:Flicker.java 16 2007-11-27 10:50:46Z prisonerjohn $
 */
public class Flicker extends AbstractAction {
	public static final int SPEED = 50;
	
	private Colorize colorize;
	private boolean isMonochrome;
    
    /** 
     * Builds a default Flicker.
     */
    public Flicker() {
        this(true);
    }
    
    /**
     * Builds a Flicker.
     * 
     * @param isMonochrome whether to use a monochrome or full spectrum range of colours
     */
    public Flicker(boolean isMonochrome) {
    	this.isMonochrome = isMonochrome;
    	setNewTargetColor();
    }
    
    /**
     * Continually assigns a new color to the TextObject.
     */
    public ActionResult behave(TextObject to) {
    	if (colorize.behave(to).complete) {
    		setNewTargetColor();
    	}
    	
    	return new ActionResult(false, false, false);
    }
    
    public void setNewTargetColor() {
    	Color randomColor;
    	if (isMonochrome) {
    		int c = (int)Math.floor(Math.random()*255);
    		randomColor = new Color(c, c, c);
    	} else {
    		int r = (int)Math.floor(Math.random()*255);
    		int g = (int)Math.floor(Math.random()*255);
    		int b = (int)Math.floor(Math.random()*255);
    		randomColor = new Color(r, g, b);
    	}
    	colorize = new Colorize(randomColor, SPEED);
    }
}
